package com.shinji.tratu;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;

public class WordDetailActivity extends SherlockFragmentActivity {
	WordDatabase wordDatabase;
	TextView mKey, mResult;
	Word word;
	static final String TAG = "WordDetailActivity";
	private AdView mAdView;
	ActionBar ab;
	AudioPlayer audioPlayer;
	Button gogoButton;
	WebView webView;
	SharedPreferences mPref;
	Editor editor; 
	boolean disableAd = false; // co vo hieu hoa quang cao khong
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_word_detail);
		int wordId = getIntent().getExtras().getInt("wordId");
		Log.i(TAG, "wordId = " + wordId);
		wordDatabase = WordDatabase.getInstance(getApplicationContext());
		word = wordDatabase.findWordById(wordId);
		Log.i(TAG, "word = " + word.getSearchResult());
		mKey = (TextView) findViewById(R.id.dic_lookup_key);
		mResult = (TextView) findViewById(R.id.dic_lookup_result);
		gogoButton = (Button) findViewById(R.id.gogo_button);	
		webView = (WebView) findViewById(R.id.webView1);
		webView.setFocusableInTouchMode(false);
		webView.setFocusable(false);
		webView.setBackgroundColor(getResources().getColor(R.color.background));
		webView.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				Log.i(TAG, "click url " + url);
				return true;
			}
		});
		
		
		mKey.setText(word.getKey());
		mResult.setText(Html.fromHtml(word.getSearchResult()));
		
		if (Constants.MOBILE_OR_NOT[word.getDicIndex()]) {
			mResult.setVisibility(View.VISIBLE);
			mResult.setText(Html.fromHtml(word.getSearchResult()));
			webView.setVisibility(View.VISIBLE);
		} else {
			webView.setVisibility(View.VISIBLE);
			webView.loadData(word.getSearchResult(),
					"text/html; charset=UTF-8", null);
			mResult.setVisibility(View.GONE);
		}
		setGogoButton(word);		
		mAdView = (AdView) findViewById(R.id.adView);
		mAdView.setAdListener(new ToastAdListener(this));
		mAdView.loadAd(new AdRequest.Builder().build());
		ab = getSupportActionBar();
		ab.setDisplayHomeAsUpEnabled(false);
		ab.setDisplayShowTitleEnabled(true);
		ab.setDisplayUseLogoEnabled(true);
		ab.setHomeButtonEnabled(true);
		ab.setDisplayShowHomeEnabled(true);
		audioPlayer = new AudioPlayer(getApplicationContext());
		audioPlayer.setButton(gogoButton);
		audioPlayer.setDataSource(word.getServerAudioLink());

		//
		Tracker t = ((AnalyticsSampleApp) this.getApplication())
				.getTracker(com.shinji.tratu.AnalyticsSampleApp.TrackerName.APP_TRACKER);
		t.setScreenName(TAG);
		t.send(new HitBuilders.AppViewBuilder().build());
	}

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.activity_word_detail, menu);
//		return true;
//	}


	private void setGogoButton(Word result) {
		if (result.getServerAudioLink() != null
				&& result.getServerAudioLink().length() > 0) {
			gogoButton.setVisibility(View.VISIBLE);
			gogoButton.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Log.i(TAG, "clicked");
					audioPlayer.playAudio();
				}
			});
		}
	}
	
	@Override
	protected void onPause() {
		if (!disableAd)
			mAdView.pause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		disableAd = ToastAdListener.isAdDisable(this);
		if (disableAd) {
			mAdView.setVisibility(View.GONE);
			Log.i(TAG, "disable ad");
		} else {
			mAdView.setVisibility(View.VISIBLE);
			mAdView.resume();
		}
	}

	@Override
	protected void onDestroy() {
		if (!disableAd)
			mAdView.destroy();
		super.onDestroy();
	}

}
